package com.kecheng.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import com.kecheng.com.yx.util.WeixinUtil;

public class KefuUtil {
	//发送卡卷/图文/音乐/视频待实现
	
	//发动语音消息
	public static void kefuVoice(String openid,String media_id) throws Exception{
		String voice = "{\"touser\":\""+openid+"\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\""+media_id+"\"}}";
		send(voice);
	}
	//发送图片消息
	public static void kefuImg(String openid,String media_id) throws Exception{
		String img = "{\"touser\":\""+openid+"\",\"msgtype\":\"image\",\"image\":{\"media_id\":\""+media_id+"\"}}";
		send(img);
	}
	//发送文本消息
	 public static void kefuText(String openid,String content) throws Exception{
		 String text = "{\"touser\":\""+openid+"\",\"msgtype\":\"text\",\"text\":{\"content\":\""+content+"\"}}";
		 send(text);
	 }
	 public static String send(String xml) throws Exception{
		  String accessToken = WeixinUtil.getAccessToken().getToken();
		  String urlurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+accessToken;
		  byte[] c;
		  c=xml.getBytes("utf-8");
		  xml = new String(c,"utf-8");
		  System.out.println("xml:"+xml);
		  try{  
	          // Configure and open a connection to the site you will send the request  
	          URL url = new URL(urlurl);  
	          URLConnection urlConnection = url.openConnection();  
	          // 设置doOutput属性为true表示将使用此urlConnection写入数据  
	          urlConnection.setDoOutput(true);  
	          // 定义待写入数据的内容类型，我们设置为application/x-www-form-urlencoded类型  
	          urlConnection.setRequestProperty("content-type", "application/xml");  
	          // 得到请求的输出流对象  
	          OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream(),"utf-8");  
	          // 把数据写入请求的Body  
	          out.write(xml);
	          out.flush();  
	          out.close();  
	          // 从服务器读取响应  
	          InputStream inputStream = urlConnection.getInputStream();  
	          String encoding = urlConnection.getContentEncoding();
	          
	          String body = IOUtils.toString(inputStream, "utf-8");  
	          System.out.println(body);
	  	      
	      }catch(IOException e){  
	      	e.printStackTrace();
	      } 
	      return null;
	  }
	 
	 /** 
	     * 扩展xstream，使其支持CDATA块 
	     *  
	     * @date 2013-05-19 
	     */  
	    private static XStream xstream = new XStream(new XppDriver() {  
	        public HierarchicalStreamWriter createWriter(Writer out) {  
	            return new PrettyPrintWriter(out) {  
	                // 对所有xml节点的转换都增加CDATA标记   
	                boolean cdata = true;  
	  
	                @SuppressWarnings("unchecked")  
	                public void startNode(String name, Class clazz) {  
	                    super.startNode(name, clazz);  
	                }  
	  
	                protected void writeText(QuickWriter writer, String text) {  
	                    if (cdata) {  
	                        writer.write("<![CDATA[");  
	                        writer.write(text);  
	                        writer.write("]]>");  
	                    } else {  
	                        writer.write(text);  
	                    }  
	                }  
	            };  
	        }  
	    }); 

}
